# -*- coding:UTF-8 -*-

import sqlite3
import logging

logging.basicConfig(level=logging.INFO)


class SqliteData:
    def __init__(self):
        self.sqconn = sqlite3.connect("./confile/sqlitedata.db", check_same_thread=False)
        self.sqcor = self.sqconn.cursor()
        self._create_dayport_data()

    def _create_dayport_data(self):
        sql = """
        create table if not exists dayport_data(
        daytime text,
        type text,
        dataid  text,
        old_v    text,
        newest_v    text
        );
        """
        self.sqcor.execute(sql)
        self.sqconn.commit()

    def insert_data(self, table, value, coloum=''):
        try:
            sql = "insert into {} {} values{};".format(table, coloum, tuple(value))
            logging.info(sql)
            self.sqcor.execute(sql)
            self.sqconn.commit()
        except BaseException as e:
            logging.error(e)

    def select_data(self, table, showcols, condition):
        sql = "select {} from {} where {};".format(showcols, table, condition)
        logging.info(sql)
        self.sqcor.execute(sql)
        return self.sqcor.fetchall()

    def count_data(self, table, condition):
        sql = "select count(1) from {} where {} ;".format(table, condition)
        self.sqcor.execute(sql)
        return self.sqcor.fetchall()
